define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        // {field: 'group.name', title: __('Group')},
                        { field: 'acct_id', title: __('平台'), searchList: Config.adminlist, formatter: Table.api.formatter.normal },
                        { field: 'new_nickname', title: __('Nickname'), operate: false },

                        { field: 'nickname', title: __('Nickname'), visible: false, operate: 'LIKE' },
                        { field: 'is_channel', title: __('是否经销商'), formatter: Table.api.formatter.label, searchList: { 0: __('否'), 1: __('是') }},

                        { field: 'puser.new_nickname', title: __('推荐人'), operate: false },
                        { field: 'puser.nickname', title: __('推荐人'), visible: false, operate: 'LIKE' },
                        { field: 'lev.name', title: __('分销等级'), operate: 'LIKE' },

                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'code', title: __('邀请码'), operate: 'LIKE' },

                        { field: 'sales', title: __('销量'), operate: false, sortable: true },
                        { field: 'total_fee', title: __('销售金额(元)'), operate: false, sortable: true },
                        { field: 'commission', title: __('可用佣金'), operate: false, sortable: true },
                        { field: 'contact', title: __('Contact'), operate: 'LIKE' },
                        { field: 'contact_phone', title: __('Contact_phone'), operate: 'LIKE' },

                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        { field: 'id_card', title: __('身份证'), operate: 'LIKE' },
                        { field: 'city', title: __('省市区'), operate: 'LIKE' },
                        { field: 'address', title: __('通讯地址'), operate: 'LIKE' },

                        { field: 'name', title: __('真实姓名'), operate: 'LIKE' },

                        { field: 'is_real', title: __('是否实名'), formatter: Table.api.formatter.label, searchList: { 0: __('否'), 1: __('是') } },

                        // { field: 'imagex', title: __('身份证正面'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        // { field: 'imagey', title: __('身份证反面'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        // { field: 'face_image', title: __('人脸照片'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'sign1_image', title: __('实名认证协议'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'sign1_image', title: __('实名认证协议'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'sign5_image', title: __('渠道商协议'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },




                        { field: 'is_commission', title: __('是否分销'), formatter: Table.api.formatter.label, searchList: { 0: __('否'), 1: __('是') }, visible: Config.acct_id == 0 ? true : false },

                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        { field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'order', //唯一标识、权限标识
                                    dropdown: '', //按钮下拉分组，默认为不分组
                                    text: '订单信息', //按钮显示的文字，支持function
                                    title: '订单信息', //按钮显示的文字，支持function
                                    classname: 'btn btn-info btn-xs btn-dialog', //按钮的class，支持btn-dialog/btn-ajax/btn-addtabs
                                    // icon: 'fa fa-plus', //按钮的图标
                                    url: function (row) {
                                        return 'shop/order/index?user_id=' + row.id;
                                    }, //按钮的链接，支持使用{字段名}来占位替换，支持`function`
                                    // hidden: function(row){}, //判断按钮是否隐藏
                                }
                                //     {
                                //         name: 'setagent', //唯一标识、权限标识
                                //         dropdown: '', //按钮下拉分组，默认为不分组
                                //         text: '设为服务商', //按钮显示的文字，支持function
                                //         title: '设为服务商', //按钮显示的文字，支持function
                                //         classname: 'btn btn-info btn-xs btn-ajax btn-detail', //按钮的class，支持btn-dialog/btn-ajax/btn-addtabs
                                //         // icon: 'fa fa-plus', //按钮的图标
                                //         url: 'user/user/setagent', //按钮的链接，支持使用{字段名}来占位替换，支持`function`
                                //         confirm: '是否设为服务商？', //点击按钮后的确认框，支持`function`
                                //         refresh: false, //操作完成后是否刷新列表
                                //         // disable: function(row){}, //判断按钮是否禁用
                                //         visible: function(row){
                                //             if(row.is_agent == 1){
                                //                 return false;
                                //             }else{
                                //                 return true;
                                //             }
                                //         }, //判断按钮是否可见
                                //         // hidden: function(row){}, //判断按钮是否隐藏
                                //         extend: '', //扩展的扩展属性
                                //     },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        sedit: function () {
            Controller.api.bindevent();
        },
        cuser: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/cuser',
                    // add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                search: false,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        // {field: 'group.name', title: __('Group')},
                        { field: 'new_nickname', title: __('Nickname'), operate: false },
                        { field: 'nickname', title: __('Nickname'), visible: false, operate: 'LIKE' },

                        { field: 'puser.new_nickname', title: __('推荐人'), operate: false },
                        { field: 'puser.nickname', title: __('推荐人'), visible: false, operate: 'LIKE' },
                        { field: 'lev.name', title: __('分销等级'), operate: 'LIKE' },

                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'code', title: __('邀请码'), operate: 'LIKE' },

                        { field: 'commission', title: __('可用佣金'), operate: false, sortable: true },
                        { field: 'xtotal', title: __('总业绩'), operate: false },
                        { field: 'utotal', title: __('人数'), operate: false },

                        { field: 'contact', title: __('Contact'), operate: 'LIKE' },
                        { field: 'contact_phone', title: __('Contact_phone'), operate: 'LIKE' },

                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        { field: 'id_card', title: __('身份证'), operate: 'LIKE' },
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        { field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'add-detail', //唯一标识、权限标识
                                    dropdown: '', //按钮下拉分组，默认为不分组
                                    text: '用户列表', //按钮显示的文字，支持function
                                    title: '用户列表', //按钮显示的文字，支持function
                                    classname: 'btn btn-info btn-xs btn-dialog btn-detail', //按钮的class，支持btn-dialog/btn-ajax/btn-addtabs
                                    // icon: 'fa fa-plus', //按钮的图标
                                    url: 'user/user/suser/channel/{ids}', //按钮的链接，支持使用{字段名}来占位替换，支持`function`
                                    // confirm: '是否同意该请求？', //点击按钮后的确认框，支持`function`
                                    refresh: false, //操作完成后是否刷新列表
                                    // disable: function(row){}, //判断按钮是否禁用
                                    // visible: function(row){}, //判断按钮是否可见
                                    // hidden: function(row){}, //判断按钮是否隐藏
                                    extend: '', //扩展的扩展属性
                                },

                            ]
                        }
                    ]
                ]
            });

            Form.events.daterangepicker($("form[role=form]"))


            table.on('post-body.bs.table', function () {
                $(".btn-detail").data("area", ["100%", "100%"]);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        suser: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/suser',
                    // add_url: 'user/user/add',
                    edit_url: 'user/user/sedit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'acct_id', title: __('平台'), searchList: Config.adminlist, formatter: Table.api.formatter.normal },

                        { field: 'channel', title: __('channel'), visible: false },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'puser.nickname', title: __('推荐人'), operate: 'LIKE' },
                        // { field: 'lev.name', title: __('分销等级'), operate: 'LIKE' },

                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        // { field: 'code', title: __('邀请码'), operate: 'LIKE' },

                        // { field: 'commission', title: __('可用佣金'), operate: false, sortable: true },
                        // { field: 'contact', title: __('Contact'), operate: 'LIKE' },
                        { field: 'proportion', title: __('渠道商扶持有效率(%)'), operate: 'LIKE' },
                        { field: 'custom_proportion', title: __('客户扶持有效率(%)'), operate: 'LIKE' },

                        { field: 'channel_level', title: __('经销商等级'), searchList: { 1: __('经销商'), 2: __('区域经销商'), 3: __('总经销商') }, formatter: Table.api.formatter.label },
                        // { field: 'id_card', title: __('身份证'), operate: 'LIKE' },
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        { field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name: 'allocate',
                                    text: __('分配'),
                                    title: __('分配'),
                                    classname: 'btn btn-primary btn-xs btn-ajax',
                                    confirm: '是否分配？',
                                    url: 'user/user/allocate',
                                    refresh: true,
                                },
                                {
                                    name: 'allocate_custom',
                                    text: __('分配客户'),
                                    title: __('分配客户'),
                                    classname: 'btn btn-primary btn-xs btn-ajax',
                                    confirm: '是否分配客户？',
                                    url: 'custompool/rateallocation',
                                    refresh: true,
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});